package com.yuyang.gulimall.order.listener;

import com.rabbitmq.client.Channel;
import com.yuyang.gulimall.order.entity.OrderEntity;
import com.yuyang.gulimall.order.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author yu yang
 * @date 2023/2/9 -19:26
 */
@Service
@RabbitListener(queues = {"order.release.order.queue"})
public class OrderCloseListener {
    @Autowired
    OrderService orderService;
    @RabbitHandler
    public void listenerOrder(Message message, OrderEntity order, Channel channel) throws IOException {
        System.out.println("订单已过期，关闭订单 订单号==》"+order.getOrderSn());
        try {
            orderService.closeOrder(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
